﻿/****************************** Module Header ******************************\
 * Module Name:  mxStrip.cs
 * Module Version:  1.0.0 (2012-8-8)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for mxStrip. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;

namespace UCL.Forms
{
    /// <summary>
    /// 工具栏扩展方法
    /// </summary>
    public static class mxStrip
    {
        private static ToolStripItem currentMenuItem;

        /// <summary>
        /// 设置菜单项目鼠标事件的图片效果
        /// </summary>
        /// <param name="mi"></param>
        /// <param name="imgNormal"></param>
        /// <param name="bgMove"></param>
        /// <param name="bgClick"></param>
        /// <param name="imgNormals"></param>
        public static void SetImage(this ToolStripItem mi, Image imgNormal, Image imgMove, Image imgClick = null, Image[] imgNormals = null)
        {
            mi.MouseEnter += delegate(object sender, EventArgs e) { ((ToolStripItem)sender).Image = imgMove; };
            mi.Click += delegate(object s, EventArgs e)
            {
                currentMenuItem = mi;
                if (imgNormals == null || imgClick == null || imgNormals.Length != mi.Owner.Items.Count) return;

                int i = 0;
                foreach (ToolStripItem m in mi.Owner.Items)
                {
                    m.Image = imgNormals[i];
                    i++;
                }
                mi.Image = imgClick;
            };
            mi.MouseLeave += delegate(object sender, EventArgs e)
            {
                if (currentMenuItem != null && currentMenuItem.Equals(mi)) return;
                ((ToolStripItem)sender).Image = imgNormal;
            };
        }

        /// <summary>
        /// 批量添加图形菜单(菜单只有一个图片)
        /// <para>2012-9-11</para>
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="rm"></param>
        /// <param name="imagekeys"></param>
        public static void AddImageMenus(this MenuStrip ms, ResourceManager rm, string[] imagekeys)
        {
            ms.Renderer = new MenuItemNoBorderRender();
            ToolStripMenuItem tsmi;
            Image imgMove, imgNormal, imgClick;
            Bitmap[] imgNormals = new Bitmap[imagekeys.Length];
            for (int i = 0; i < imagekeys.Length; i++)
            {
                imgNormals[i] = (Bitmap)rm.GetObject(imagekeys[i]);
            }
            foreach (string key in imagekeys)
            {
                imgNormal = (Bitmap)rm.GetObject("{0}".Fmt(key));
                imgMove = (Bitmap)rm.GetObject("{0}_".Fmt(key));
                imgClick = (Bitmap)rm.GetObject("{0}_".Fmt(key));
                tsmi = new ToolStripMenuItem() { ImageScaling = ToolStripItemImageScaling.None, DisplayStyle = ToolStripItemDisplayStyle.Image,Name = key };
                tsmi.Image = imgNormal;
                tsmi.SetImage(imgNormal, imgMove, imgClick, imgNormals);
                ms.Items.Add(tsmi);
            }
        }

        /// <summary>
        /// 批量添加图形菜单(菜单只有一个图片)
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="imagekeys"></param>
        /// <remarks>2014-2-20 by sutroon</remarks>
        public static void AddImageMenus(this MenuStrip ms, string[] imagekeys)
        {
            ms.Renderer = new MenuItemNoBorderRender();
            ToolStripMenuItem tsmi;
            Image imgMove, imgNormal, imgClick;
            Image[] imgNormals = new Image[imagekeys.Length];

            for (int i = 0; i < imagekeys.Length; i++)
            {
                imgNormals[i] = SoSkinHelper.GetImageById(imagekeys[i]);
            }
            foreach (string key in imagekeys)
            {
                imgNormal = SoSkinHelper.GetImageById(key);
                imgMove = SoSkinHelper.GetImageHoverById(key);
                imgClick = SoSkinHelper.GetImageHoverById(key);
                tsmi = new ToolStripMenuItem() { ImageScaling = ToolStripItemImageScaling.None, DisplayStyle = ToolStripItemDisplayStyle.Image, Name = key };
                tsmi.Image = imgNormal;
                tsmi.SetImage(imgNormal, imgMove, imgClick, imgNormals);
                ms.Items.Add(tsmi);
            }
        }

        /// <summary>
        /// 获取右键菜单选项的容器控件
        /// <para>如果是多级菜单，务必在初始化之前设置:cmsObject.Tag=theSourceControl;</para>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="mi"></param>
        /// <returns></returns>
        public static T GetSourceControl<T>(this ToolStripItem mi) where T : Control
        {
            if (mi.OwnerItem == null) return (T)((ContextMenuStrip)mi.Owner).SourceControl;
            int i = 99;
            while (i > 0)
            {
                if (mi.OwnerItem == null) break;
                mi = mi.OwnerItem;
                i--;
            } 
            return (T)((ContextMenuStrip)mi.Owner).Tag;
        }

        /// <summary>
        /// 在按钮旁边打开右键菜单
        /// </summary>
        /// <param name="cms"></param>
        /// <param name="c"></param>
        /// <param name="offsetx"></param>
        /// <param name="offsety"></param>
        public static void DisplayWithControl(this ContextMenuStrip cms, Control c,int offsetx=16,int offsety=16)
        {
            cms.Show(c, new Point(offsetx, offsety));
        }
    }
}
